﻿ var _table =null;
$(document).ready(function(){

	    var $table = $('#table-user');
	    _table = $table.dataTable($.extend(true,{},CONSTANT.DATA_TABLES.DEFAULT_OPTION, {
		 orderMulti: false, //启用多列排序
		 bFilter: false,//去掉搜索框  
		 order: [], //取消默认排序查询,否则复选框一列会出现小箭头
		 renderer: "bootstrap", //渲染样式：Bootstrap和jquery-ui
		 pagingType: "simple_numbers", //分页样式：simple,simple_numbers,full,full_numbers
		 aaSorting: [[ 1, "desc" ]],//默认第几个排序
		 bStateSave: true,//状态保存
			pageLength:10,
			lengthMenu:[10,20,50,100],
			iDisplayLength: 10,
	        ajax : function(adata, callback, settings) {//ajax配置为function,手动调用异步查询
	            //封装请求参数
	           var param = userManage.getQueryCondition(adata);
	            $.ajax({
	                    type: "post",
	                    url: "/eoas-cms-web/user/userList",
	                    cache : false,  //禁用缓存
	                    data: param,    //传入已封装的参数
	                    dataType: "json",
	                    success: function(reslut) {
                            //封装返回数据，这里仅演示了修改属性名
                            var returnData = {};
                            returnData.draw = adata.draw;//这里直接自行返回了draw计数器,应该由后台返回
                            returnData.recordsTotal = reslut.data.total;
							returnData.recordsFiltered = reslut.data.total;//后台不实现过滤功能，每次查询均视作全部结果
                            returnData.data = reslut.data.list;

                            callback(returnData);
	                    },
	                    error: function(XMLHttpRequest, textStatus, errorThrown) {
	                       console.log("查询失败");
	                    }
	                });
	        },
    "columns": [
	    	 {"data":  function ( row, type, set ) {//属性方法可以自定义显示方式
	             return  '<input type="checkbox" class="iCheck" value='+row.id+'>';        
	         },"bSortable":false},
            { "data": "uuid","bSortable": false},
            { "data": "loginName"},
            { "data": "password","bSortable": false},
            { "data": "realName","bSortable": false},
            { "data": "salt","bSortable": false},
            { "data": "userType","bSortable": false},
            { "data":"status","bSortable": false},
     ],
     "columnDefs": [
    	 {
             "targets": [6], // 目标列位置，下标从0开始
             "data": "userType", // 数据列名
             "render": function(data, type, full) { // 返回自定义内容
                 return "<span style='color:red;'>" + data + "</span>";
             }
         },
         {
             "targets": [7], // 目标列位置，下标从0开始
             "data": "status", // 数据列名
             "render": function(data, type, full) { // 返回自定义内容
            	 if("0"==data){
            		return  '<span class="label label-default">已停用</span>';
            	 }
                 return '<span class="label label-success">已启用</span>';
             }
         },
         {
             "targets": [8], // 目标列位置，下标从0开始
             "data": "id", // 数据列名
             "render": function(data, type, full) { // 返回自定义内容
            	 var str ="";
            	 if("1"==full.status){
            		 str ='<a style="text-decoration:none" onClick="admin_stop(this,'+data+')" href="javascript:void(0);" title="禁用">';
            	 }else{
            		 str ='<a style="text-decoration:none" onClick="admin_start(this,'+data+')" href="javascript:void(0);" title="启用">';
            	 }
            	 
            	 str =str+'<i class="Hui-iconfont">&#xe615;</i></a> <a title="编辑" href="javascript:void(0);" onclick="admin_add(\'管理员编辑\',\'user-add?id='+data+'\',\'700\',\'700\')" class="ml-5" style="text-decoration:none"><i class="Hui-iconfont">&#xe6df;</i></a> <a title="删除" href="javascript:void(0);" onclick="admin_del(this,'+data+')" class="ml-5" style="text-decoration:none"><i class="Hui-iconfont">&#xe6e2;</i></a>';
            	 return str;
                
             }
         }
     ]
	 })).api();       
	
	
	 $("#btn-advanced-search").click(function(){
		 refulsh();
	  });
	
});



 
var userManage = {
    getQueryCondition : function(data) {
        var param = {};
//        //组装排序参数
//        if (data.order&&data.order.length&&data.order[0]) {
//            switch (data.order[0].column) {
//            case 1:
//                param.orderColumn = "name";
//                break;
//            case 2:
//                param.orderColumn = "uuid";
//                break;
//            case 3:
//                param.orderColumn = "status";
//                break;
//            case 4:
//                param.orderColumn = "start_date";
//                break;
//            default:
//                param.orderColumn = "name";
//                break;
//            }
//            param.orderDir = data.order[0].dir;
//        }
//        //组装查询参数
//        if (userManage.fuzzySearch) {
//            param.fuzzy = $("#fuzzy-search").val();
//        }else{
            param.datemin = $("#datemin").val();
            param.datemax = $("#datemax").val();
            param.param = $("#param").val();
//        }

        //组装分页参数
        param.page = data.start;
        param.rows = data.length;
// 		param.draw = data.draw;

        return param;
    },
   
}

/*管理员-增加*/
function admin_add(title,url,w,h){
	layer_show_callback(title,url,w,h,function(){
		refulsh();
	});
}
/*管理员-删除*/
function admin_del(obj,id){
	layer.confirm('确认要删除吗？',function(index){
		ajaxRequestJson("deleteUserManager",{
			"id":id
		},function(result){
			refulsh();
			layer.msg('已删除!', {icon: 6,time:1000});
		})
		
	});
}

/*管理员-编辑*/
function admin_edit(title,url,id,w,h){
	layer_show(title,url,w,h);
}

/*管理员-停用*/
function admin_stop(obj,id){
	layer.confirm('确认要停用吗？',function(index){
		//此处请求后台程序，下方是成功后的前台处理……
		ajaxRequestJson("updateUserManager",{
			"id":id,
			"status":0
		},function(result){
			refulsh();
			layer.msg('已停用!', {icon: 6,time:1000});
		})
		
	});
}

/*管理员-启用*/
function admin_start(obj,id){
	layer.confirm('确认要启用吗？',function(index){
		//此处请求后台程序，下方是成功后的前台处理……
		ajaxRequestJson("updateUserManager",{
			"id":id,
			"status":1
		},function(result){
			refulsh();
			layer.msg('已启用!', {icon: 6,time:1000});
		})		
	});
}

function refulsh(){
	userManage.fuzzySearch = false;
    _table.draw();
}


